package com.medicine.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medicine.common.config.RabbitMQConfig;
import com.medicine.common.param.OrderParam;
import com.medicine.product.entity.Category;
import com.medicine.product.mapper.CategoryMapper;
import com.medicine.product.param.CategoryParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class CategoryService extends ServiceImpl<CategoryMapper, Category> {

    @Autowired
    CategoryMapper categoryMapper;


    public Page<Category> selectList(CategoryParam categoryParam){
        Page<Category> categoryPage = new Page<>(categoryParam.getPage(),categoryParam.getSize());
        return (Page<Category>) page(categoryPage,new QueryWrapper<Category>().lambda().like(Category::getName,categoryParam.getName()));
    }



    public List<Category> listWithTree() {
        //1、查出所有分类
        List<Category> entities = baseMapper.selectList(null);

        //2、组装成父子的树形结构

        //2.1）、找到所有的一级分类
        List<Category> level1Menus = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid().equals(0)
        ).map((menu)->{
            menu.setChildren(getChildrens(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());




        return level1Menus;
    }


    //递归查找所有菜单的子菜单
    private List<Category> getChildrens(Category root,List<Category> all){

        List<Category> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //2、菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }


    public void addorupdate(Category category){
        if(StringUtils.isNotEmpty(category.getId())){
            updateById(category);
        }else{
            save(category);
        }
    }

    @RabbitListener(queues = "item_queue")
    public void msg(String msg){
        System.out.println("消费者消费消息了0："+msg);
        //TODO 这里可以做异步的工作
    }

    @RabbitListener(queues = RabbitMQConfig.ORDER_QUEUE)
    public void msgs(String msg){
        System.out.println("消费者消费消息了1："+msg);
        //TODO 这里可以做异步的工作
    }

}
